import {
	getToken,
	selectByuserIdList,
	selectById,
	editChildUser,
	selectallrelation,
	addChildUser,
	removeChildUser,
	addChildRoleRegulateTb,
	delChildRoleRegulateTb,
	upaChildRoleRegulateTb,
} from '@/api/api.js';
import {
	formModule
} from '@/utils/formdata.js'
var fromdata = new formModule();
const state = {
	scheduleList:[],
	reationList:[],
	statuheight: '40',
	currentUser: {
		userId: 27,
	},
	userInfo: {

	},
	islogin: false,
}
const actions = {
	// 设置电池栏高度
	async setstatusheight({
		commit,
		state
	}, data) {
		let height = await uni.getSystemInfoSync()
		console.log('电池栏高度', height.statusBarHeight);
		commit('setheight', height.statusBarHeight)
	},
	// 获取亲友团关系
	async getreation({
		commit,
		state
	}, pageSize) {
		let result = await selectallrelation({
			pageNum: 1,
			pageSize: pageSize?pageSize:100,
			userId:state.currentUser.userId
		});
		result = fromdata.formatDate(result);
		commit('changeReation', result);
	},
	// 删除日程表
	async deleteschedule({
		commit,
		state
	},id){
		console.log(id);
		let result =  await removeChildUser({
			ids:id
		})
		result = fromdata.formatDate(result);
		if(result.data && result.data.data ==1){
			return true
		}else{
			return false
		}
	},
	// 修改日程表权限
	async upaRoleRegulateTb({
		commit,
		state
	},data){
		let {childId,regulateId} = data;
		let result =  await upaChildRoleRegulateTb({
			childId,
			regulateId,
			// state.currentUser.userId
			userId:20
		})
		result = fromdata.formatDate(result);
		if(result.data && result.data.code==1){
			return true
		}else{
			return false
		}
	},
	// 删除成员
	async delRoleRegulateTb({
		commit,
		state
	},data){
		let {childId} = data;
		let result =  await delChildRoleRegulateTb({
			childId,
			userId:state.currentUser.userId,
		})
		result = fromdata.formatDate(result);
		if(result.data && result.data.data ==1){
			return true
		}else{
			return false
		}
	},
	// 设置日程列表
	async SetSchedule({
		commit,
		state
	}, data) {
		if (data && data.length > 0) {
			commit('changeSchedule', data);
		} else {
			let result = await selectByuserIdList({
				userId: state.currentUser.userId
			})
			result = fromdata.formatDate(result);
			if(state.scheduleList.length>0 && result.length>0){
				let arr =JSON.parse(JSON.stringify(state.scheduleList));
				result.map(item=>{
					arr.map(vals=>{
						if(item.id == vals.id){
							item.check == vals.check?vals.check:false
						}
					})
					return result
				})
			}else{
				result.map((item,index)=>{
					if(index ==0){
						item.check = true
					}else{
						item.check = false
					}
					return result
				})
			}
			commit('changeSchedule', result);
			return state.scheduleList;
		}
	},
	// 获取单个日程表
	async getIdSchedule({
		commit,
		state
	}, data) {
		let result = await selectById(data)
		result = fromdata.formatDate(result);
		return result;
	},
	// 更改单个日程
	async updateSchedule({
		commit,
		state
	}, data) {
		let result = await editChildUser(data)
		result = fromdata.formatDate(result);
		return result;
	},
	// 添加日程
	async addSchedule({
		commit,
		state
	}, data) {
		let result = await addChildUser(data)
		result = fromdata.formatDate(result);
		return result;
	},
	// 
	getName: async ({
		commit,
		state
	}, data) => {
		console.log('获取值', state.name)
		return state.name
	},
	// 获取token
	get_Token: async ({
		commit,
		state
	}, data) => {
		console.log('获取token值', data, state)
		let result = await getToken(data);
		commit('setToken', result.data.data)
		// state.token  = result.data.data
		return state.token;
	}
}
// 不支持return
const mutations = {
	changeReation(state, data){
		state.reationList = data;
	},
	setheight(state, data) {
		state.statuheight = data;
	},
	// 设置user信息
	setUserInfo(state, data) {
		state.userInfo = data;
	},
	setToken: (state, data) => state.token = data,
	changeSchedule(state, value) {
		state.scheduleList = value;
	},
	setName: (state, name) => {
		state.name = name;
		console.log(`updata name is ${state.name}`)
	}
}
export default {
	namespaced: true,
	state,
	actions,
	mutations
}
